import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMail {

	public static void main(String[] args) throws Exception {
		sendEmail(
			"mail.interbgc.com", 
			"sender@somehost.com", 
			"recipient@somehost.com");
		System.out.println("Email successfully sent.");
	}

	public static void sendEmail(String smtpServer, String sender, String recipient) 
	throws MessagingException, AddressException {
		// Create a session
		Properties props = new Properties();
		props.put("mail.smtp.host", smtpServer);
		Session session = Session.getDefaultInstance(props, null);
		
		// Create the message
		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(sender));
		message.addRecipient(Message.RecipientType.TO, 
			new InternetAddress(recipient));
		message.setSubject("Hello from JavaMail");
		message.setText("This mail was sent by JavaMail");
		 
		// Send the message
		Transport.send(message);
	}

}
